package cn.rkylin.oms.warehouse.stock.dao;

import cn.rkylin.core.IDataBaseFactory;
import cn.rkylin.oms.warehouse.stock.domain.BatchStockLog;
import cn.rkylin.oms.warehouse.stock.domain.Stock;
import cn.rkylin.oms.warehouse.stock.domain.StockLog;
import cn.rkylin.oms.warehouse.stock.vo.BatchStockVO;
import cn.rkylin.oms.warehouse.stock.vo.StockVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 库存DAO实现类
 *
 * @author liming
 * @date 2017/10/16
 */
@Repository(value = "stockDAO")
public class StockDAOImpl implements IStockDAO {
    @Autowired
    protected IDataBaseFactory dao;

    private static final String STATEMENT_PAGE_SELECT_BY_CONDITION = "pageSelectStock";

    @Override
    public StockVO getStock(StockVO stockVO) throws Exception {
        List<StockVO> list = dao.findAllList("selectByPrimaryKeyStor", stockVO);

        if (list != null && list.size() > 0) {
            return list.get(0);
        }

        return null;
    }

    @Override
    public List<StockVO> getSkuList(StockVO stockVO) throws Exception {
        return dao.findAllList("selectSkuList", stockVO);
    }

    @Override
    public int updateStock(Stock stock) throws Exception {
        return dao.update("updateByPrimaryKeySelectiveStor", stock);
    }

    @Override
    public int updateStock2(Stock stock) throws Exception {
        return dao.update("updateByPrimaryKeySelectiveStor2", stock);
    }

    @Override
    public int insertStock(Stock stock) throws Exception {
        return dao.insert("insertSelectiveStor", stock);
    }

    @Override
    public int insertStockLog(StockLog stockLog) throws Exception {
        return dao.insert("insertSelectiveStockLog", stockLog);
    }

    /**
     * 获取未分派数
     *
     * @param stockVO
     * @return
     * @throws Exception
     */
    @Override
    public StockVO getUnallotQty(StockVO stockVO) throws Exception {
        StockVO stock = dao.findOne("selectUnallotQtyByPrimaryKeyStor", stockVO);
        return stock;
    }

    /**
     * selectPagedByCondition：根据查询条件获取库存信息列表
     */
    @Override
    public PageInfo<StockVO> selectPagedByCondition(int page, int length, StockVO queryParamVO) throws Exception {
        return dao.findPage(page, length, STATEMENT_PAGE_SELECT_BY_CONDITION, queryParamVO);
    }

    /**
     * 获取商品库存 用于加权成本计算 wangxing add
     *
     * @param stockVO
     * @return
     * @throws Exception
     */
    @Override
    public StockVO selectSkuStockBySkuId(StockVO stockVO) throws Exception {
        StockVO stock = dao.findOne("selectSkuStockBySkuId1", stockVO);
        return stock;
    }

    /**
     * 获取批次库存信息
     *
     * @param batchStockVO
     * @return
     * @throws Exception
     */
    @Override
    public BatchStockVO getBatchStock(BatchStockVO batchStockVO) throws Exception {
        List<BatchStockVO> list = dao.findAllList("selectByPrimaryKeyBatchStock", batchStockVO);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 插入批次库存
     *
     * @param batchStockVO
     * @return
     * @throws Exception
     */
    @Override
    public int insertBatchStock(BatchStockVO batchStockVO) throws Exception {
        return dao.insert("insertSelectiveBatchStock", batchStockVO);
    }

    /**
     * 更新批次库存（增量）
     *
     * @param batchStockVO
     * @return
     * @throws Exception
     */
    @Override
    public int updateBatchStock(BatchStockVO batchStockVO) throws Exception {
        return dao.update("updateByPrimaryKeySelectiveBatchStock", batchStockVO);
    }

    /**
     * 插入批次库存日志
     *
     * @param batchStockLog
     * @return
     * @throws Exception
     */
    @Override
    public int insertBatchStockLog(BatchStockLog batchStockLog) throws Exception {
        return dao.insert("insertSelectiveBatchStockLog", batchStockLog);
    }
}
